package com.lry.netty01.server.queue;

import com.lry.netty01.server.entity.Packet;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class TaskQueue {

    private  LinkedBlockingQueue<Task> blockingQueue = new LinkedBlockingQueue<Task>();

    //线程工厂
    private  ThreadFactory factory = new ThreadFactory() {
        AtomicInteger atomic = new AtomicInteger(1);
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r,"TASK_QUEUE_THREAD--"+atomic.getAndIncrement());
            return thread;
        }
    };

    private  ThreadPoolExecutor exec = new ThreadPoolExecutor
            (100,
                    1000,
                    5,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    factory,
                    new ThreadPoolExecutor.AbortPolicy());

    public void offerTask(Task task){
        if(null==task)
            return;
        blockingQueue.offer(task);
    }


    public  void exec(){
        while(true){
            try {
                exec.execute(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }






}
